package com.joysuch.wwyt.workflow.repository;

import com.joysuch.wwyt.workflow.entity.WorkFlowScreenConfig;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
 * @Author ZhangDong
 * @Date 2020/11/4 16:04
 */
public interface WorkFlowScreenConfigDao extends JpaRepository<WorkFlowScreenConfig, Long> {

    WorkFlowScreenConfig findFirstByProcessId(Long processId);

    List<WorkFlowScreenConfig> findByProcessIdIn(List<Long> processIds);

    List<WorkFlowScreenConfig> findByTypeId(Long id);

    @Query(value = "SELECT DISTINCT p.biz_type_name FROM wwyt_workflow_screen_config c ," +
            " wwyt_workflow_process p  WHERE c.process_id=p.id  AND (c.environment_point_flag is null or c.environment_point_flag =0) AND type_id= ?1 AND p.location_service_open =1 AND p.delete_flag = '0'", nativeQuery = true)
    List<String> findBizTypeNameListByTypeIdAndEnvironmentFlagIsNull(Long typeId);

    @Query(value = "SELECT DISTINCT p.biz_type_name FROM wwyt_workflow_screen_config c ," +
            " wwyt_workflow_process p  WHERE c.process_id=p.id  AND (c.environment_point_flag is null or c.environment_point_flag =0)  AND p.location_service_open =1 AND p.delete_flag = '0'", nativeQuery = true)
    List<String> findBizTypeNameListByEnvironmentFlagIsNull();

    @Query(value = "SELECT DISTINCT p.biz_type_name FROM wwyt_workflow_screen_config c ," +
            " wwyt_workflow_process p  WHERE c.process_id=p.id AND  p.delete_flag = '0'", nativeQuery = true)
    List<String> getAllBizTypeName();

    @Query(value = "SELECT * FROM wwyt_workflow_screen_config WHERE delete_flag = '0' ORDER BY show_order ", nativeQuery = true)
    List<WorkFlowScreenConfig> findAllByShowOrder();

    @Query(value = "SELECT * FROM wwyt_workflow_screen_config WHERE biz_type_code =?1 and environment_point_flag =?2 and delete_flag = '0' ", nativeQuery = true)
    WorkFlowScreenConfig findByBizTypeCodeAndEnvironmentPointFlag(String code, int i);

    @Query(value = "SELECT *  FROM wwyt_workflow_screen_config  WHERE type_id = ?1 AND environment_point_flag in (?2) AND delete_flag = '0'", nativeQuery = true)
    List<WorkFlowScreenConfig> findBizTypeNameListByTypeIdAndEnvironmentPointFlag(Long id, List<Integer> environmentPointFlag);


    @Query(value = "SELECT *  FROM wwyt_workflow_screen_config  WHERE type_id in (?1) AND environment_point_flag in (?2) AND delete_flag = '0'", nativeQuery = true)
    List<WorkFlowScreenConfig> findBizTypeNameListByTypeIdsAndEnvironmentPointFlag(List<Long> ids, List<Integer> environmentPointFlag);

    @Modifying
    @Query(value = "UPDATE wwyt_workflow_screen_config SET delete_flag = '1' WHERE biz_type_code =?1 AND environment_point_flag =?2", nativeQuery = true)
    void deleteByBizTypeCodeAndEnvironmentPointFlag(String code, int i);

    @Query(value = "SELECT * FROM wwyt_workflow_screen_config WHERE environment_point_flag = ?1 and delete_flag = '0' ORDER BY show_order ", nativeQuery = true)
    List<WorkFlowScreenConfig> findAllByEnviromentFlag(int i);

    @Query(value = "SELECT * FROM wwyt_workflow_screen_config WHERE environment_point_flag in (?1) and delete_flag = '0' ORDER BY show_order ", nativeQuery = true)
    List<WorkFlowScreenConfig> findAllByEnviromentFlagList(List<Integer> types);
}

